# -*- coding:utf-8 -*-
#Python
#Generator: PyCharm
#Author: Zhong


from emp import models

def toInt(str):
    str = str.strip()
    if not str:
        return None
    return int(str)

def saveCate(order, cate):
    models.Category(name=cate, order=order).save()

def saveEmp(dep, arr):
    '''
    dep<str>: Employee's Department
    arr<list>: 序号	岗位名称	姓名 内线	 外线 手机	短号	住宅电话	备注
    return True if success
    '''

    for i in arr:
        print i,

    employee = models.Employee(order=arr[0].strip(), post=arr[1].strip(), name=arr[2].strip(),
                               inner=toInt(arr[3].strip()),
                               outer=toInt(arr[4].strip()), mobile=toInt(arr[5].strip()),
                               short_tel=toInt(arr[6].strip()), telephone=toInt(arr[7].strip()), remark=arr[8].strip())
    employee.save()
    print employee.id
    employee.department = models.Department.objects.get_or_create(name=dep.strip())
    return True

def saveDep(cate, arr):
    cate = models.Category.objects.get(name=cate)
    index = 0
    for i in arr:
        print i, cate
        dep = models.Department(name=i, order=index)
        dep.save()
        dep.category = [cate]
        dep.save()
        index+=1


def showAll():
    cates = models.Category.objects.all()
    cates.empCount = models.Employee.objects.count()
    for cate in cates:
        cate.deps = cate.department_set.all()
        for dep in cate.deps:
            dep.emps = dep.employee_set.all()
            #print cate,dep,dep.emps.all()

    return cates        
